package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.cache.service.ISheQuWenDaStatisVOService;
import cn.wolfcode.luowowo.cache.vo.SheQuWenDaStatisVo;
import cn.wolfcode.luowowo.comment.domain.SheQuWenDa;
import cn.wolfcode.luowowo.comment.domain.WenTiHuiDa;
import cn.wolfcode.luowowo.comment.query.communityQuery;
import cn.wolfcode.luowowo.comment.service.ISheQuWenDaService;
import cn.wolfcode.luowowo.exception.LogicException;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.util.AjaxResult;
import cn.wolfcode.luowowo.website.annotation.RequiredLogin;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import cn.wolfcode.luowowo.website.util.UMEditorUploader;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.jar.JarEntry;


@Controller
@RequestMapping("/community")
public class CommunityController {
    @Reference
    private IDestinationService destinationService;
    @Reference
    private ISheQuWenDaService sheQuWenDaService;
    @Reference
    private ISheQuWenDaStatisVOService sheQuWenDaStatisVOService;
    @Reference
    private IUserInfoService userInfoService;

    //进入问答主页
    @RequestMapping("")
    public String index(Model model, SheQuWenDa sheQuWenDa, String problemId) {
        List<SheQuWenDa> list = sheQuWenDaService.selectWenTi(sheQuWenDa);
        model.addAttribute("wenTi", list);
        List<Map> userInfos = sheQuWenDaStatisVOService.getgoldMedal();
        model.addAttribute("userInfos",userInfos);
        return "community/wenda";
    }

    //进入提问页面
    @RequestMapping("/Question")
    public String question() {

        return "community/public";
    }

    //获取城市
    @RequestMapping("/city")
    @ResponseBody
    public Object city(int deep) {
        return AjaxResult.SUCCESS.addData(destinationService.getDestsByDeep(deep));
    }

    //我要提问
    @RequestMapping("/tiwen")
    @ResponseBody
    @RequiredLogin
    public AjaxResult tiwen(@UserParam UserInfo userInfo, SheQuWenDa sheQuWenDa) {
        if (userInfo == null) {
            throw new LogicException("请先登录");
        }
        Destination destination = destinationService.getWenDaCityName(sheQuWenDa.getCity().longValue());
        sheQuWenDa.setCityName(destination.getName());
        sheQuWenDa.setHeadUrl(userInfo.getHeadImgUrl());
        sheQuWenDa.setUserId(userInfo.getId());
        sheQuWenDa.setLevel(userInfo.getLevel());
        sheQuWenDa.setNickName(userInfo.getNickname());
        String id = sheQuWenDaService.save(sheQuWenDa);
        return AjaxResult.SUCCESS.addData(id);
    }

    //进入回答页面
    @RequestMapping("/answer")
    public String answer(Model model, SheQuWenDa sheQuWenDa, @UserParam UserInfo userInfo, Long userId) {
        SheQuWenDa answer = sheQuWenDaService.selectHuiDa(sheQuWenDa);
        model.addAttribute("list", answer);
//        UserInfo info = userInfoService.get(userInfo.getId());
//        model.addAttribute("user", info);
        List<Long> listUserId = userInfoService.getAttentionId(userInfo.getId());
        model.addAttribute("listUserId", listUserId);
        sheQuWenDaStatisVOService.addViewNum(sheQuWenDa.getId());
        List<WenTiHuiDa> huida = sheQuWenDaService.queryWenTiHuiDa(sheQuWenDa.getId());
        model.addAttribute("daan", huida);
        return "community/wendaDetail";
    }

    //问答页面顶
    //problemId:问题id
    @RequestMapping("/thumbsupnum")
    @ResponseBody
    public AjaxResult thumbsupnum(String problemId, @UserParam UserInfo userInfo) {
        if (userInfo == null) {
            AjaxResult result = new AjaxResult(false, "请先登录");
            result.setCode(102);
            return result;
        }
        if (!sheQuWenDaStatisVOService.addThumbsupnum(problemId, userInfo.getId())) {
            return new AjaxResult(false, "今天已经顶过,请明天在顶");
        }
        return AjaxResult.SUCCESS;
    }

    //回答提问
    @RequestMapping("/answerComment")
    @ResponseBody
    public Object answerComment(WenTiHuiDa wenTiHuiDa, @UserParam UserInfo user) {
        wenTiHuiDa.setUserInfo(user);
        sheQuWenDaService.saveReply(wenTiHuiDa, user.getId());
        return AjaxResult.SUCCESS;
    }


//    //排行榜
//    @RequestMapping("/Leaderboard")
//    @ResponseBody
//    public AjaxResult Leaderboard(communityQuery qo){
//        if (qo.getLeaderboardId() == 0){
//            List<>sheQuWenDaStatisVOService.jinpai(qo);
//        }else {
//
//        }
//    }

    @RequestMapping("/attention")
    @ResponseBody
    public AjaxResult attention(Model model, @UserParam UserInfo userInfo, Long statusId, Long userId) {
        if (userInfo == null) {
            throw new LogicException("请先登录");
        }
        Boolean attention = userInfoService.saveOrDelete(userInfo, statusId, userId);
//        AjaxResult result = null;
        if (attention == true) {
            AjaxResult result = new AjaxResult(true, "关注成功");
            return result;
        } else {
            AjaxResult result = new AjaxResult(false, "已取消关注");
            return result;
        }
//        return AjaxResult.SUCCESS.addData(result);
    }


    @Value("${file.path}")
    private String basePath;

    @RequestMapping("/contentImage")
    @ResponseBody
    public String uploadUEImage(MultipartFile upfile, HttpServletRequest request) throws Exception {
        UMEditorUploader up = new UMEditorUploader(request);
        String[] fileType = {".gif", ".png", ".jpg", ".jpeg", ".bmp"};
        up.setAllowFiles(fileType);
        up.setMaxSize(10000); //单位KB
        up.upload(upfile, basePath);

        String callback = request.getParameter("callback");
        String result = "{\"name\":\"" + up.getFileName() + "\", \"originalName\": \"" + up.getOriginalName() + "\", \"size\": " + up.getSize()
                + ", \"state\": \"" + up.getState() + "\", \"type\": \"" + up.getType() + "\", \"url\": \"" + up.getUrl() + "\"}";
        result = result.replaceAll("\\\\", "\\\\");
        if (callback == null) {
            return result;
        } else {
            return "<script>" + callback + "(" + result + ")</script>";
        }

    }
}